package cn.itcast.service;

import cn.itcast.CheckItemDao;
import cn.itcast.CheckItemService;
import cn.itcast.pojo.CheckGroup;
import cn.itcast.pojo.CheckItem;
import cn.itcast.pojo.PageResult;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {

    @Autowired
    private CheckItemDao checkItemDao;

    @Override
    public void add(CheckItem checkItem) {
        checkItemDao.add(checkItem);
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckItem> page = checkItemDao.findPage(queryString);
        return new PageResult(page.getTotal(), page.getResult());


    }

    @Override
    public void delete(Integer id) {
        Long count = checkItemDao.findByCheckItemId(id);

        if (count > 0) {
            throw new RuntimeException("当前检查项被引用 不能删除");

        }

        checkItemDao.delete(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemDao.edit(checkItem);
    }

    @Override
    public CheckItem findById(long id) {
      return   checkItemDao.findById(id);
    }

    @Override
    public List<CheckItem> findAll() {
        List<CheckItem> checkItemList= checkItemDao.findAll();
        return checkItemList;
    }


}

